﻿/**
 * Created by Guochunjiang on 2018/11/25.
 */
(function () {
    cce.Region = function (i,points, fillStyle, strokeStyle) {
        this.i = i;
        cce.DisplayObject.call(this);
        this.points = points || null;
        this.fillStyle = fillStyle || "";
        this.strokeStyle = strokeStyle || "";
        this.minX = 0;
        if (points != null) {
            this.minX = points[0].x;
            for (var i = 0; i < points.length; i++) {
                if (this.minX > points[i].x) {
                    this.minX = points[i].x;
                }
            }
        }
    };

    cce.Region.prototype = Object.create(cce.DisplayObject.prototype);
    cce.Region.prototype.constructor = cce.Region;

    cce.Region.prototype.draw = function () {
        this.context.save();
        //开始路径
        this.context.beginPath();
        var lineArr = [];
        var points = this.points;
        for (var i = 0; i < points.length ; i++) {
            this.context.lineWidth = 5
            this.context.lineTo(points[i].x, points[i].y);
        }
        //路径闭合
        this.context.closePath();
        if (this.strokeStyle.length != 0) {
            this.context.strokeStyle = this.strokeStyle;
            this.context.lineJoin = 'round';

        }
        if (this.fillStyle.length != 0) {
            this.context.fillStyle = this.fillStyle;
            this.context.fill();
        }
        this.context.restore();

    };

    cce.Region.prototype.compareTo = function (target) {
        if (target.minX == null) {
            return null;
        }
        if (this.minX < target.minX) {
            return -1;
        }
        if (this.minX == target.minX) {
            return 0;
        }
        if (this.minX > target.minX) {
            return 1;
        }
        return null;
    };

    cce.Region.prototype.comparePointX = function (point) {
        if (point.x == null) {
            return null;
        }
        if (this.minX < point.x) {
            return -1;
        }
        if (this.minX == point.x) {
            return 0;
        }
        if (this.minX > point.x) {
            return 1;
        }
        return null;
    };

    cce.Region.prototype.hasPoint = function (target) {

        if (target.x == null || target.y == null) {
            return false;
        }
        var isIn = false;

        this.context.save();
        this.context.beginPath();

        //console.log(this.points);
        this.context.moveTo(this.points[0].x, this.points[0].y);

        for (var i = 1; i < this.points.length; i++) {
            this.context.lineTo(this.points[i].x, this.points[i].y);
        }

        if (this.context.isPointInPath(target.x, target.y)) {
            isIn = true;
        }
        this.context.closePath();
        this.context.restore();
        return isIn;
    };

}());